package demo.user.controller;


import demo.user.dto.UserDTO;
import demo.user.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.management.StringValueExp;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LonelySnow
 * @since 2020-06-15
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 增加一条
     * @param userDTO
     * @return
     */
    @PostMapping("/addOne")
    @ApiOperation("新增用户接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加用户成功"),
            @ApiResponse(code = 400, message = "请求参数错误")
    })
    public ResponseEntity<Boolean> addOne(
            @ApiParam(value = "新增用户数据") UserDTO userDTO) {
        boolean result = userService.addOne(userDTO);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改一条数据
     * @param userDTO
     * @param id
     * @return
     */
    @PutMapping("/updateOne")
    @ApiOperation(value = "根据ID修改用户接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 401, message = "用户无权限")
    })
    public ResponseEntity<Boolean> updateOne(
            @ApiParam(value = "修改的用户数据") UserDTO userDTO,
            @ApiParam(value = "修改用户ID") @RequestParam("id") Long id) {
        boolean result = userService.updateOne(userDTO, id);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除一条
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOne")
    @ApiOperation(value = "根据ID删除用户接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 401, message = "用户无权限")
    })
    public ResponseEntity<Void> deleteOne(
            @ApiParam(value = "删除用户id", example = "1") @RequestParam("id") Long id) {
        userService.deleteOne(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询全部用户接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 401, message = "用户无权限")
    })
    public ResponseEntity<Map<String, UserDTO>> listAll() {
        List<UserDTO> result = userService.selectAll();
        Map<String, UserDTO> resultMap= new HashMap<>();
        int i = 1;
        for (UserDTO userDTO : result) {
            resultMap.put(String.valueOf(i), userDTO);
            i++;
        }
        return ResponseEntity.ok(resultMap);
    }

    /**
     * 查询一条
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    @ApiOperation(value = "根据id查询用户接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 401, message = "用户无权限")
    })
    public ResponseEntity<UserDTO> findOne(
            @ApiParam(value = "查询用户ID") @RequestParam("id") Long id) {
        UserDTO result = userService.selectOne(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据用户名查询一条数据
     * @param userName
     * @return
     */
    @GetMapping("query")
    @ApiOperation("根据用户名密码查询用户接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 401, message = "用户无权限")
    })
    public ResponseEntity<UserDTO> userLogin(
            @ApiParam(value = "用户名") @RequestParam("username") String userName,
            @ApiParam(value = "密码") @RequestParam("password") String password) {
        UserDTO userDTO = userService.userLogin(userName, password);
        return ResponseEntity.ok(userDTO);
    }

}

